package com.guw.AWT;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//内部类的方法,更加面向对象
//内部类的最大好处,就是可以畅通无阻的访问外部的属性和方法
public class TestCalc3 {
    public static void main(String[] args) {
        new Calculator3().loadFrame();

    }
}

//计算器类
class Calculator3 extends Frame {
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        //1个标签
        Label label = new Label("+");
        //绑定监听事件,在此处使用了内部类
        button.addActionListener(new MyCalculatorListener3());

        //布局:流式布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);

        //调用关闭窗口的方法,可以用了!
        windowClose(this);
    }

    //监听器类,内部类
    private class MyCalculatorListener3 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //获得加数和被加数
            int n1 = Integer.parseInt(num1.getText());
            //将这两个值+运算之后,放到第三个框
            int n2 = Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            //清除前两个框的数值
            num1.setText("");
            num2.setText("");

        }
    }
    //关闭窗体的事件监听方法
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}



